package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.net.Uri;
import ohos.vibrator.agent.VibratorAgent;
import ohos.vibrator.bean.VibrationPattern;

import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import static com.ohos.carbon.ResourceTable.Graphic_background_color_white;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class PowerMenuSlice extends AbilitySlice {


    private Image back_button, startup, refresh, aircraft, shock, sound;
    private Text text_title, web, color_text1, color_text2;
    private Component dialog_color, componentById, layout1, layout2, layout3, red, yellow, green, shock_layout, sound_layout, dialog_layout;
    private Button dialog;
    private Boolean Click_shock = false, dialog_ = true;
    private Boolean Click_sound = false;
    private int max;
    private RoundProgressBar roundProgressBar;
    private int progress_fristThread = 1;
    private AnimatorProperty animator;
    private Thread myFristThread, mySecondThread;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_power_menu);

        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Power Menu")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Power Menu")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Power Menu", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Power Menu", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Power Menu", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Power Menu");
        roundProgressBar = (RoundProgressBar) findComponentById(ResourceTable.Id_round_progress_bar);

        this.componentById = findComponentById(ResourceTable.Id_titlebar);
        this.componentById.setBackground(ElementScatter.getInstance(getContext()).parse(Graphic_background_color_white));
        web = (Text) findComponentById(ResourceTable.Id_web);
        color_text1 = (Text) findComponentById(ResourceTable.Id_color_text1);
        color_text2 = (Text) findComponentById(ResourceTable.Id_color_text2);
        dialog = (Button) findComponentById(ResourceTable.Id_dialog);

        layout1 = findComponentById(ResourceTable.Id_layout1);
        layout2 = findComponentById(ResourceTable.Id_layout2);
        layout3 = findComponentById(ResourceTable.Id_layout3);
        dialog_color = findComponentById(ResourceTable.Id_dialog_color);

        red = findComponentById(ResourceTable.Id_red);
        yellow = findComponentById(ResourceTable.Id_yellow);
        green = findComponentById(ResourceTable.Id_green);
        shock_layout = findComponentById(ResourceTable.Id_shock_layout);
        sound_layout = findComponentById(ResourceTable.Id_sound_layout);
        dialog_layout = findComponentById(ResourceTable.Id_dialog_layout);

        startup = (Image) findComponentById(ResourceTable.Id_startup);
        refresh = (Image) findComponentById(ResourceTable.Id_refresh);
        aircraft = (Image) findComponentById(ResourceTable.Id_aircraft);
        shock = (Image) findComponentById(ResourceTable.Id_shock);
        sound = (Image) findComponentById(ResourceTable.Id_sound);

//        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_white_carbon_uparrow));
        red.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_red));
        startup.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_startup));
        yellow.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_yellow));
        refresh.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_refresh_24px));
        green.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_green));
        aircraft.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_airplanemode_off_24px));
        shock.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_vibration_24px));
        sound.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_volume_up_24px));
        back_button.setClickedListener(component -> onBackPressed());
        animator = roundProgressBar.createAnimatorProperty();


        web.setClickedListener(component -> {
            Intent intent = new Intent();
            Uri uri = Uri.parse(web.getText());
            intent.setUri(uri);
            startAbility(intent);
        });

        dialog.setClickedListener(component -> {
            dialog_layout.setVisibility(Component.VISIBLE);
            dialog_color.setVisibility(Component.HIDE);
            web.setClickable(false);
            progress_fristThread = 1;
        });

        layout1.setClickedListener(component -> {
            dialog_layout.setVisibility(Component.HIDE);
            dialog_color.setVisibility(Component.VISIBLE);
            dialog_color.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_back_red));
            color_text1.setText("Power Off");
            color_text2.setText("Shutting down...");
            animator.setDuration(1200).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
            myFristThread = new FirstThread();
            myFristThread.start();
            new EventHandler(EventRunner.getMainEventRunner()).postTask(new TimerTask() {
                @Override
                public void run() {
                    dialog_layout.setVisibility(Component.HIDE);
                    dialog_color.setVisibility(Component.HIDE);
                }
            }, 3000);
        });

        layout2.setClickedListener(component -> {
            dialog_layout.setVisibility(Component.HIDE);
            dialog_color.setVisibility(Component.VISIBLE);
            dialog_color.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_back_yellow));
            color_text1.setText("Reboot");
            color_text2.setText("Rebooting...");
            animator.setDuration(1200).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
            myFristThread = new FirstThread();
            myFristThread.start();
            new EventHandler(EventRunner.getMainEventRunner()).postTask(new TimerTask() {
                @Override
                public void run() {
                    dialog_layout.setVisibility(Component.HIDE);
                    dialog_color.setVisibility(Component.HIDE);
                }
            }, 3000);
        });

        layout3.setClickedListener(component -> {
            dialog_layout.setVisibility(Component.HIDE);
            dialog_color.setVisibility(Component.VISIBLE);
            dialog_color.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_back_green));
            color_text1.setText("Airplane Mode");
            color_text2.setText("Switching Mode...");
            animator.setDuration(1200).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).start();
            myFristThread = new FirstThread();
            myFristThread.start();

            new EventHandler(EventRunner.getMainEventRunner()).postTask(new TimerTask() {
                @Override
                public void run() {
                    dialog_layout.setVisibility(Component.HIDE);
                    dialog_color.setVisibility(Component.HIDE);
                }
            }, 3000);
        });

        shock_layout.setClickedListener(component -> {
            if (Click_shock) {
                shock_layout.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_white_back));
                shock.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_vibration_24px));
                Click_shock = false;
            } else {
                shock_layout.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_darkgray));
                shock.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_vibration_on_24px));
                Click_shock = true;

                VibratorAgent vibratorAgent = new VibratorAgent();

                // 查询硬件设备上的振动器列表
                List<Integer> vibratorList = vibratorAgent.getVibratorIdList();
                if (vibratorList.isEmpty()) {
                    return;
                }
                int vibratorId = vibratorList.get(0);

                // 查询指定的振动器是否支持指定的振动效果
                boolean isSupport = vibratorAgent.isEffectSupport(vibratorId,
                        VibrationPattern.VIBRATOR_TYPE_CAMERA_CLICK);

                // 创建指定效果的一次性振动
                vibratorAgent.startOnce(vibratorId,
                        VibrationPattern.VIBRATOR_TYPE_CAMERA_CLICK);

                // 创建指定振动时长的一次性振动
                int vibratorTiming = 1000;
                vibratorAgent.startOnce(vibratorId, vibratorTiming);
            }
        });
        sound_layout.setClickedListener(component -> {
            if (Click_sound) {
                sound_layout.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_white_back));
                sound.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_volume_up_24px));
                Click_sound = false;
            } else {
                sound_layout.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_darkgray));
                sound.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_volume_up__on24px));
                Click_sound = true;
            }
        });
    }

    class FirstThread extends Thread {
        @Override
        public void run() {
//            super.run();
            while (progress_fristThread < 51) {
                try {
                    roundProgressBar.setProgressValue(progress_fristThread++);
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (roundProgressBar.getProgress() == 50) {
                animator.alpha(0).setDuration(1000).start();
                animator.reset();
                web.setClickable(true);
            }
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        dialog_layout.setVisibility(Component.HIDE);
    }

    @Override
    protected void onBackPressed() {
        if (dialog_layout.getVisibility() == Component.VISIBLE) {
            dialog_layout.setVisibility(Component.HIDE);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
